package com.eds.service;

import com.eds.log.LogUtil;
import com.eds.manager.RecommendationTaskManager;
import com.eds.pojo.RecommendationDTO;
import com.eds.redis.RedisRecommendationCacheService;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName：AsyncRecommendationService
 * @Author: vvSmile
 * @Date: 2025/04/09
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Service
public class AsyncRecommendationService {
    private static final Logger logger = LogUtil.getLogger(AsyncRecommendationService.class);

    @Resource
    private RedisRecommendationCacheService cacheService;
    @Resource
    private RecommendationTaskManager taskManager;
    @Resource
    private AiService aiService;

    //异步更新用户推荐
    public void updateUserRecommendationsAsync(Long userId) {
        logger.info("提交异步任务：更新用户{}的推荐数据", userId);
        //提交任务到任务管理器中
        taskManager.submitTask(() -> {
            try {
                logger.info("开始异步更新用户推荐，用户ID：{}", userId);
                List<RecommendationDTO> recommendationsForUser = aiService.getRecommendationsForUser(userId);
                cacheService.cacheUserRecommendations(userId, recommendationsForUser);
                logger.info("异步更新用户推荐成功，用户ID：{}", userId);
            } catch (Exception e) {
                logger.error("异步更新用户推荐失败，用户ID：{}", userId, e);
            }
        });
    }

    //异步更新热门推荐
    public void updateHotRecommendationsAsync() {
        logger.info("提交异步任务：更新热门推荐数据");
        //提交任务到任务管理器中
        taskManager.submitTask(() -> {
            try {
                logger.info("开始异步更新热门推荐数据");
                List<RecommendationDTO> hotRecommendations = aiService.getHotRecommendations();
                cacheService.cacheHotRecommendations(hotRecommendations);
                logger.info("异步更新热门推荐数据成功");
            } catch (Exception e) {
                logger.error("异步更新热门推荐数据失败", e);
            }
        });
    }


    //异步更新开心推荐
    public void updateHappyRecommendationsAsync() {
        logger.info("提交异步任务：更新开心推荐数据");
        taskManager.submitTask(() -> {
            try {
                logger.info("开始异步更新开心一刻数据");
                List happyRecommendations = aiService.getHappyRecommendations();
                cacheService.cacheHappyRecommendations(happyRecommendations);
            } catch (Exception e) {
                logger.error("异步更新开心推荐数据失败", e);
            }
        });
    }
}
